package com.edu.hre.db.sql.pim;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreEmpEducation;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.pim.EmpEducationForm;
import com.edu.hre.struts.form.pim.EmpEducationListForm;

public class EmpEducationDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "empEducationDAOService";
	private BaseService baseService  = null;
	 
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	} 

	public ArrayList<EmpEducationForm> getEmpEducationList(int limit, int offset, int empNumber){

		ArrayList<EmpEducationForm> listResult = new ArrayList<EmpEducationForm>();

		String sql = "SELECT eedu.id AS col1," +
					" edu.name AS col2," +
					" eedu.institute AS col3," +
					" maj.name AS col4," +
					" eedu.year AS col5, " +
					" eedu.score AS col6, " +
					" eedu.start_date AS col7, " +
					" eedu.end_date AS col8, " +
					" eedu.emp_number AS col9 " +

					" FROM hre_emp_education AS eedu" +
					" LEFT OUTER JOIN hre_education AS edu ON eedu.education_id = edu.id" +
					" LEFT OUTER JOIN hre_employee AS emp ON eedu.emp_number = emp.emp_number " +
					" LEFT OUTER JOIN hre_major AS maj ON eedu.major = maj.id " +

					" WHERE emp.is_deleted = 0 " +
					" AND emp.emp_number = ? " +
					" ORDER BY eedu.date_created DESC";
		
		List <Object[]> listObject = baseService.listArrayObject(sql, limit, offset, empNumber);
		for(int i = 0; listObject != null && i < listObject.size(); i++){

			try {

				Object[] objs = listObject.get(i);
				EmpEducationForm edu = new EmpEducationForm();

				edu.setId(Integer.parseInt(CommonUtils.normalizeObjectToString(objs[1], "0")));
				edu.setEmpNumber(empNumber);
				edu.setLevel(CommonUtils.normalizeObjectToString(objs[2], ""));
				edu.setInstitute(CommonUtils.normalizeObjectToString(objs[3], ""));
				edu.setMajor(CommonUtils.normalizeObjectToString(objs[4], ""));
				edu.setYear(CommonUtils.normalizeObjectToString(objs[5], ""));
				edu.setScore(CommonUtils.normalizeObjectToString(objs[6], ""));
				
				try {
					edu.setStartDate(CommonUtils.dateToString((Date) objs[7]));
					edu.setEndDate(CommonUtils.dateToString((Date) objs[8]));
				} catch (Exception e) {
					CommonUtils.println(e.getMessage());
				}

				listResult.add(edu);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		
		return listResult;
	}

	public int countEmpEducations(int empNumber){

		int count = baseService.countByColumns(new String[]{"empNumber"}, new int[]{Compare.EQUAL}, new Integer[]{empNumber}, "id", HreEmpEducation.class);
		return count < 0 ? 0 : count;
	}

	public void deleteEducations(EmpEducationListForm form){

		ArrayList<EmpEducationForm> info = (ArrayList<EmpEducationForm>) form.getAllEducations();

		for (int i = 0; i < info.size(); i++) {

			try {

				EmpEducationForm work = info.get(i);
				if (work.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, work.getId(), null, null, 0, HreEmpEducation.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
